<template>
  <el-dialog
    :title="!dataForm.sscId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-form-item label="阈值" prop="threshold">
        <el-input v-model.number="dataForm.threshold" placeholder="" class="iptslt"></el-input>
      </el-form-item>
      <el-form-item label="告警等级" prop="alarmLevel">
        <el-select v-model="dataForm.alarmLevel" placeholder="请选择告警等级" class="iptslt">
          <el-option label="1、紧急告警" :value="1"></el-option>
          <el-option label="2、主要警告" :value="2"></el-option>
          <el-option label="3、次要告警" :value="3"></el-option>
          <el-option label="4、提示告警" :value="4"></el-option>
          <el-option label="5、不确定告警" :value="5"></el-option>
          <el-option label="6、清楚告警" :value="6"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="dataForm.alarmType" placeholder="请选择告警类型" class="iptslt">
          <el-option label="CPU" value="0"></el-option>
          <el-option label="内存" value="1"></el-option>
           <el-option label="网卡" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="成熟时间(分钟)" prop="matureTime">
        <el-input v-model="dataForm.matureTime" placeholder="" class="iptslt"></el-input>
      </el-form-item>
      <el-form-item label="告警间隔时差(分钟)" prop="alarmGaptime">
        <el-input v-model="dataForm.alarmGaptime" placeholder="" class="iptslt"></el-input>
      </el-form-item>
      <el-form-item label="告警升级(次数)" prop="alarmUpgrade">
        <el-input v-model="dataForm.alarmUpgrade" placeholder="" class="iptslt"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sscId: 0,
          threshold: '', // 阈值
          alarmLevel: '', // 告警等级
          alarmType: '', // 告警类型
          matureTime: '',  // 成熟时间
          alarmGaptime: '', // 告警间隔
          alarmUpgrade: '' // 告警升级
        },
        dataRule: {
          threshold: [
            { required: true, message: '阈值不能为空', trigger: 'blur' },
            { min: 1, max: 1000000, type:'number', message: '长度在 1 到 100 个数字', trigger: 'blur' }
          ],
          alarmLevel: [
            { required: true, message: '告警等级不能为空', trigger: 'change' }
          ],
          alarmType: [
            { required: true, message: '告警类型不能为空', trigger: 'change' }
          ],
          matureTime: [
            { required: true, message: '成熟时间(分钟)不能为空', trigger: 'blur' }
          ],
          alarmGaptime: [
            { required: true, message: '告警间隔时差(分钟)不能为空', trigger: 'blur' }
          ],
          alarmUpgrade: [
            { required: true, message: '告警升级(次数)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sscId = id || 0
        if (this.dataForm.sscId) {
          console.log('true')
        } else {
          console.log('false')
        }
        console.log(this.dataForm.sscId, '4444')
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sscId) {
            this.$http({
              url: this.$http.preBaseUrl(`/cs/alarmRule/info/${this.dataForm.sscId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.threshold = data.rule.threshold
                this.dataForm.alarmLevel = data.rule.alarmLevel
                this.dataForm.alarmType = data.rule.alarmType
                this.dataForm.matureTime = data.rule.matureTime
                this.dataForm.alarmGaptime = data.rule.alarmGaptime
                this.dataForm.alarmUpgrade = data.rule.alarmUpgrade
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.preBaseUrl(`/cs/alarmRule/${!this.dataForm.sscId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.sscId || undefined,
                'threshold': this.dataForm.threshold,
                'alarmLevel': this.dataForm.alarmLevel,
                'alarmType': this.dataForm.alarmType,
                'matureTime': this.dataForm.matureTime,
                'alarmGaptime': this.dataForm.alarmGaptime,
                'alarmUpgrade': this.dataForm.alarmUpgrade
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
  .iptslt {
    width: 300px;
  }
</style>

